1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.base;
18
19 import com.google.common.annotations.GwtCompatible;
20
21 import junit.framework.TestCase;
22
23
24
25
26
27
28 @GwtCompatible(emulated = true)
29 public class ObjectsTest extends TestCase {
30
31 public void testEqual() throws Exception {
32 assertTrue(Objects.equal(1, 1));
33 assertTrue(Objects.equal(null, null));
34
35
36 String s1 = "foobar";
37 String s2 = new String(s1);
38 assertTrue(Objects.equal(s1, s2));
39
40 assertFalse(Objects.equal(s1, null));
41 assertFalse(Objects.equal(null, s1));
42 assertFalse(Objects.equal("foo", "bar"));
43 assertFalse(Objects.equal("1", 1));
44 }
45
46 public void testHashCode() throws Exception {
47 int h1 = Objects.hashCode(1, "two", 3.0);
48 int h2 = Objects.hashCode(
49 new Integer(1), new String("two"), new Double(3.0));
50
51 assertEquals(h1, h2);
52
53
54 assertTrue(Objects.hashCode(1, 2, null) != Objects.hashCode(1, 2));
55 assertTrue(Objects.hashCode(1, 2, null) != Objects.hashCode(1, null, 2));
56 assertTrue(Objects.hashCode(1, null, 2) != Objects.hashCode(1, 2));
57 assertTrue(Objects.hashCode(1, 2, 3) != Objects.hashCode(3, 2, 1));
58 assertTrue(Objects.hashCode(1, 2, 3) != Objects.hashCode(2, 3, 1));
59 }
60
61 public void testFirstNonNull_withNonNull() throws Exception {
62 String s1 = "foo";
63 String s2 = Objects.firstNonNull(s1, "bar");
64 assertSame(s1, s2);
65
66 Long n1 = new Long(42);
67 Long n2 = Objects.firstNonNull(null, n1);
68 assertSame(n1, n2);
69 }
70
71 public void testFirstNonNull_throwsNullPointerException() throws Exception {
72 try {
73 Objects.firstNonNull(null, null);
74 fail("expected NullPointerException");
75 } catch (NullPointerException expected) {
76 }
77 }
78 }
79